#import <NSObject.h>

@interface ObjectTest:NSObject
{
	int a;
	int b;
	int c;
	int _e;
}

@property int d;
@property int e;

@end

@implementation ObjectTest

@synthesize e = _e;
-(void)setD:(int)newd
{
	_d = newd+1;
}

@end

struct  Date
{
	int year;
	int month;
	int day;
};
struct Date day1 = {.year=2015,.month=1,.day=13};
struct Date day2 = {2015,1,13};

NSRange range1 = {.location=2,.length=3};
NSRange range2 = {2,3};
NSRange range3 = NSMakeRange(2,3);

//表现坐标
CGPoint point = CGPointMake(20,20);//CGPoint 跨平台
NSPoint point2 = NSPointMaek(20,20);//
NSPoint point3 = CGPointMake(20,20);

//表现尺寸
NSSize size1 = NSSizeMake(100,50);//width = 100 ,heigth = 50
NSSize size2 = CGSizeMake(100,50);
CGSize size3 = CGSizeMake(100,50);
CGSize size4 = NSSizeMaek(100,50);

//UI的尺寸位置 NSRect\CGRect(CGPoint,CGSize)
CGRect rect = {0,0,100,50};
CGRect rect2 = {{0,0},{100,50}};
CGRect rect3 = {point,size1};
CGRect rect4 = {CGrectZero,CGSizeMake(100,50)};

NSLog(@"x=%f,y=%f,width=%f,height=%f",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);

NSString *pointStr = NSStringFormPoint(point);
NSString *sizeStr = NSStringFormSize(size1);
NSString *rectStr = NSStringFormRect(rect);
NSString *rangeStr = NSStringFormRange(range1);

//比较
BOOL isEqual = CGPointEqualToPoint(point,pint2);
BOOL iEqual = CGRangeEqualToRange(range1,range2);
BOOL isEqual = CGSizeEqualToSize(size1,size2);
BOOL isEqual = CGRectEqualToRect(rect,rect2);

//点是否在范围内
BOOL isInRect = CGRectContainPoint(rect2,point2);

//字符串
NSString  不可变
NSMutableString 可变长度

//集合
NSArray 不可变
NSMutableArray 可变长度

NSSet 不可变
NSMutableSet 可变长度

NSDictionary 不可变
NSMutableDictionary 可变长度

NSDate
NSObject

//NSString
NSString *str1 = @"abcdefg";
NSString *str2 = [[NSString alloc] initWithFormat:@"abcd%d",10];
NSString *str3 = [[NSString alloc] initWithUTF8String:"addadf"];
const char *str4 = [str3 UTF8String];

//char 与 NSString 互转
char *strChar = "abce";
NSString *strNSString = [[NSString alloc] initWithUTF8String:strChar];

NSString strNSString = @"abcd";
const char * strChar = [strNSString UTF8String];

//从文本里读取字符信息到字符串
NSString *fileStr = [[NSString alloc] initWithContentsOfFile:@"/User/Apple/Desktop/test.txt"
													encoding:NSUTF8StringEncodeing 
													   error:nil];

NSURL *url = [[NSURL alloc] initWithString:@"file:////User/Apple/Desktop/test.txt"];
NSURL *url = [NSURL fileURLWithPath:@"/User/Apple/Desktop/test.txt"];
NSString *fileStr = [[NSString alloc] initWithContentsOfURL:url
												   encoding:NSUTF8StringEncodeing
												   	  error:nil];

//字符串的导出
[@"write string to file" writeToFile:@"/User/Apple/Desktop/test.txt"
						  atomically:YES
						  	encoding:NSUTF8StringEncodeing
						  		erro:nil];
NSString *str = @"abdfsdfad";
[str writeToURL:url
	   encoding:NSUTF8StringEncodeing
	 atomically:YES
		  error:nil];
//这里的atomically 一般都设置为YES，应设置为YES就是当写文件的时候被中断了或者写入失败时，就不回创建文件。


//NSMutableString 使用
NSMutableString *mutableString = [NSMutableString stringWithFormat:@"abcedfaf"];
[mutableString appendString:@":adbcdefad"];

NSRange *range = [mutableString rangeOfString:@"adb"];
[mutableString deletCharactersInRange:range]; //删除 range的数据
NSLog(@"%@",mutableString); //abcedfafcdefad

NSString *a = [NSString stringWithFormat:@"adb"]; //adb
NSString *b = [a stringByAppendingString:@"adb"]; //adbadb

int a=5;
Person *person = [[Person alloc] init];
Person *persons[5] = {person,[[Person alloc] init]};

//NSArray 
NSArray *array = [NSArray array]; //创建 array
NSArray *array2 = [NSArray arrayWithObject:@"aaa"];
NSArray *array3 = [NSArray arrayWithObjects:@"aaa",@"bbb",@"ccc",nil];
NSArray *array4 = @[@"aaa",@"bbb",@"ddd"];

//NSArray 使用
[array count]; //数组数量
NSLog(@"%d",array.count);

NSLog(@"%@",[array3 objectAtIndex:2]);
NSLog(@"%@",array3[2]);

int i=0;
for(id obj in array3) //id obj 代表着每个元素
{
	NSUInteger i = [array3 indexOfObject:obj];
	NSLog(@"index:%d",i);
	if (i == 1)
	{
		break;
	}
}

-(void) enumerateObjectsUsingBlock:(void (^)(id,NSUInteger,BOOL))block;

void ^(myBlock)(id,NSUInteger,BOOL) = ^(id obj,NSUInteger index, BOOL *stop){
	NSLog(@"%d - %@",index,obj);
	if (index == 1)
	{
		*stop = YES;
	}
}
//block来遍历
[array3 enumerateObjectsUsingBlock:
^(id obj,NSUInteger index,BOOL *stop)
{
	NSLog(@"%d - %@",index,obj);
	if (index == 1)
	{//停止遍历
		*stop = YES;
	}
}]

[array3 enumerateObjectsUsingBlock:myBlock];

void ^(myBlock)(id,NSUInteger,BOOL*) = ^(id obj,NSUInteger index,BOOL *stop){
	NSLog(@"%d - %@",index,obj);
	if (index == 1)
	{//停止遍历
		*stop = YES;
	}
}

[array3 emumerateObjectsUsingBlock:myBlock];


for(int i = 0 ;i<array3.count;i++){
	BOOL isStop = NO;
	id obj = array3[i];
	myBlock(obj,i;&isStop);
	if (isStop == YES)
	{
		break;
	}
}

NSMUtableArray *arrays = [NSMutabelArray arrayWithObjects:@"aaaa",@"bbbbb",nil];
[arrays addObject:[[Person alloc]init]];
[arrays addObject:@"cccc"];
[arrays addObject:nil]; //语法错误，add objcet 不能为空

[arrays removeAllObjects];

[arrays removeObjectAtIndex:0];

//NSSet ,NSArray 区别是在：NSArray 有序，NSSet 无序

//NSSet ,不可变
NSSet *set = [NSSet set]; // 因为set不可变，所以永远都为空

//创建一个含有多个元素的set
NSSet *set2 = [NSSet setWithObjects:@"aaaa",@"bbbb",@"cccc",nil];

//随机取出一个元素
NSString *string = [set2 anyObject];

//NSMutabeSet ,可变长度
NSMutableSet *set = [NSMutableSet set];
[set addObject:@"aaaa"];
[set removeObject:[set objectAtIndex:0]];
[set removeAllObjects];

//NSDictionary
NSDictionary *dic = [NSDictionary dictionaryWithObject:@"jack" forKey:@"name"];

NSArray *keyArray = @[@"name",@"address"];
NSArray *values = @[@"jack",@"beiJin"];
NSDictionary *dic = [NSDictionary dictionaryWithObjects:values,forKeys:keyArray];

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
	@"jack",@"name",
	19,@"age",
	@"beijin",@"address",nil];

NSDictionary *dic = @{@"name":@"jack",
					  @"age":19,
					  @"address":@"beijin"}; //快速创建是编译器的特性来的，只是编译器能识别转换为标准的写法

id obj = [dic objectForKey:@"name"];

id obj = dic[@"name"] ;//快速访问

//遍历字典
NSArray *keyArray = [dic allKeys];
for (int i = 0; i < keyArray.count; ++i)
{
	NSString keyStr = keyArray[i];
	id obj = dic[keyStr];
	NSLog(@"key:%@,value:%@",keyStr,obj);
}

//遍历字典2
[dic emumerateKeysAndObjectsUsingBlock:
^(id key,id obj,BOOL *isStop){
	NSLog(@"key:%@,value:%@",key,obj);
}];

NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"jack" foKey:@"name"];//添加一个数据
[dic setObject:@"BeiJin" forKey:@"address"];
[dic setObject:18 forKey:@"age"];

[dic removeObjectForKey:@"age"]; //删除 key=@"age"的数据
[dic removeAllObjects];//删除所有的数据

//网络篇

//get
NSString *urlString = [NSString stringWithFormat:@"http://xxx.xx.com/xxx?username=%@&pwd=%@",self.name.text,self.pwd.text];
NSURL *url = [NSURL urlWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

//post
NSURL *url = [NSURL URLWithString:@"http://xxx.xxx.com/xxx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.timeoutInterval = 5.0; //设置请求超时为5s
request.HTTPMethod = @"POST";//设置请求方法
//设置请求参数
NSString *param = [NSString stringWithFormat:@"username=%@&pwd=%@",self.name.text,self.pwd.text];
//将请求参数转换为data数据，然后设置到请求内容里面
request.HTTPBody = [param dataUsingEncodeing:NSUTF8StringEncoding];

//发送同步请求
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil erro:nil];

//异步请求要实现协议的内容
...
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response; //请求得到响应
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData*) data;//请求在接收数据
- (void) connectionDidFinishLoading:(NSURLConnection *)connection;//请求已经结束，这时候可以处理接收到的数据，进行分析